﻿using System;
using System.Diagnostics;
using System.IO;
using System.Windows;
using Microsoft.Phone.Controls;
using System.Windows.Navigation;

namespace PCRemoteMobileVer
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();
        }

        
        public static SocketClient client = null;
        public static bool IsConnected = false;

        #region UI Validation
        /// <summary>
        /// Validates the txtInput TextBox
        /// </summary>
        /// <returns>True if the txtInput TextBox contains valid data, otherwise 
        /// False.
        ///</returns>
        private bool ValidatePort()
        {
            // txtInput must contain some text
            if (String.IsNullOrWhiteSpace(txtPort.Text))
            {
                MessageBox.Show("Please enter port to connect");
                return false;
            }

            return true;
        }

        /// <summary>
        /// Validates the txtRemoteHost TextBox
        /// </summary>
        /// <returns>True if the txtRemoteHost contains valid data,
        /// otherwise False
        /// </returns>
        private bool ValidateRemoteHost()
        {
            // The txtRemoteHost must contain some text
            if (String.IsNullOrWhiteSpace(txtRemoteHost.Text))
            {
                MessageBox.Show("Please enter a host name");
                return false;
            }

            return true;
        }
        #endregion

        public static void SendCommand(string data)
        {
            Debug.WriteLine("Sending " + data);
            string resulfs = client.Send(data);
            Debug.WriteLine("Result " + resulfs);
            if (!resulfs.Contains("Success"))
            {
                MessageBox.Show("Connection lost! Please check your connection!");
                IsConnected = false;
                throw new IOException();
            }
        }

        private void btnConnect_Click(object sender, RoutedEventArgs e)
        {

            // Make sure we can perform this action with valid data
            if (!IsConnected && ValidateRemoteHost() && ValidatePort())
            {
                // Instantiate the SocketClient object
                client = new SocketClient();

                string result = client.Connect(txtRemoteHost.Text, Int32.Parse(txtPort.Text));
                if (result.Equals("Success"))
                {
                    MessageBox.Show("Connection success!");
                    btnConnect.Content = "Disconnect";
                    IsConnected = true;
                    NavigationService.Navigate(new Uri("/FunctionsPage.xaml", UriKind.Relative));
                }
                else
                {
                    MessageBox.Show("Can't connect to your remote pc!");
                }
            }
            else
            {
              if (client != null)
              {
                  client.Disconnect();
                  btnConnect.Content = "Connect";
                  IsConnected = false;
              }
            }
        }
    }
}